/**
 * 
 */
package com.flying.dcss.userpermissions.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.CommonHelper;
import com.flying.dcss.app.MD5Helper;
import com.flying.dcss.userpermissions.bo.impl.DcssLoginRecord;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.bo.impl.DcssUserInfo;
import com.flying.dcss.userpermissions.dao.impl.DcssAgentInfoDAO;
import com.flying.dcss.userpermissions.dao.impl.DcssLoginRecordDAO;
import com.flying.dcss.userpermissions.dao.impl.DcssUserDAO;
import com.flying.dcss.userpermissions.dao.impl.DcssUserInfoDAO;
import com.flying.dcss.userpermissions.service.IUserService;

// TODO: Auto-generated Javadoc
/**
 * The Class UserService.
 * 
 * @author EvanHong
 */
public class UserService implements IUserService {

	/** The agent info dao. */
	private DcssAgentInfoDAO agentInfoDAO;

	/** The login record dao. */
	private DcssLoginRecordDAO loginRecordDAO;

	/** The user dao. */
	private DcssUserDAO userDAO;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.flying.dcss.userpermissions.service.IUserService#getUserById(java
	 * .lang.String)
	 */
	public DcssUser getUserById(String id) {
		// TODO Auto-generated method stub
		return userDAO.findById(id);
	}

	/**
	 * Gets the agent info dao.
	 * 
	 * @return the agent info dao
	 */
	public DcssAgentInfoDAO getAgentInfoDAO() {
		return agentInfoDAO;
	}

	/**
	 * Sets the agent info dao.
	 * 
	 * @param agentInfoDAO
	 *            the new agent info dao
	 */
	public void setAgentInfoDAO(DcssAgentInfoDAO agentInfoDAO) {
		this.agentInfoDAO = agentInfoDAO;
	}

	/**
	 * Gets the login record dao.
	 * 
	 * @return the login record dao
	 */
	public DcssLoginRecordDAO getLoginRecordDAO() {
		return loginRecordDAO;
	}

	/**
	 * Sets the login record dao.
	 * 
	 * @param loginRecordDAO
	 *            the new login record dao
	 */
	public void setLoginRecordDAO(DcssLoginRecordDAO loginRecordDAO) {
		this.loginRecordDAO = loginRecordDAO;
	}

	/**
	 * Gets the.
	 * 
	 * @return the dcss user dao
	 */
	public DcssUserDAO getUserDAO() {
		return userDAO;
	}

	/**
	 * Sets the user dao.
	 * 
	 * @param userDAO
	 *            the user dao
	 */
	public void setUserDAO(DcssUserDAO userDAO) {
		this.userDAO = userDAO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.flying.dcss.userpermissions.service.IUserService#isValidUser(com.
	 * flying.dcss.userpermissions.bo.impl.DcssUser)
	 */
	public boolean isValidUser(DcssUser user) {
		// TODO Auto-generated method stub

		// Get user data form database.
		DcssUser realUser = userDAO.findByEmail(user.getEmail());

		if (realUser != null) {

			String realPassword = realUser.getPassword();
			String currentPassword = MD5Helper.MD5Encode(user.getPassword());

			if (realPassword.equals(currentPassword)) {

				// LastLoginTime Update.
				realUser.setLastLoginTime(new Date());
				userDAO.update(realUser);

				// Save Login Record.

				return true;
			}
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.flying.dcss.userpermissions.service.IUserService#userRegister(com
	 * .flying.dcss.userpermissions.bo.impl.DcssUser)
	 */
	public DcssUser userRegister(DcssUser user) {
		// TODO Auto-generated method stub

		// Complete User.
		user.setId(UUID.randomUUID().toString());
		// Set Date.
		user.setCreateTime(new Date());
		user.setLevel(3);
		user.setUserState(0);
		user.setIsEmailVerified(0);
		user.setSecurityCode(MD5Helper.MD5Encode(user.getSecurityCode()));

		// MD5 Encoding.
		user.setPassword(MD5Helper.MD5Encode(user.getPassword()));

		userDAO.save(user);

		return user;

	}

	/**
	 * Gets the user.
	 * 
	 * @param email
	 *            the email
	 * @return the user
	 */
	public DcssUser getUserByEmail(String email) {

		DcssUser realUser = userDAO.findByEmail(email);
		realUser.setPassword("******");
		return realUser;
	}

	/**
	 * Checks if is valid email.
	 * 
	 * @param email
	 *            the email
	 * @return true, if is valid email
	 */
	public boolean isValidEmail(String email) {
		DcssUser realUser = userDAO.findByEmail(email);

		if (realUser != null) {
			return false;
		}

		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.flying.dcss.userpermissions.service.IUserService#getAllUsers()
	 */
	public List<DcssUser> getAllUsers() {
		// TODO Auto-generated method stub
		return userDAO.findAllUsers();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.flying.dcss.userpermissions.service.IUserService#verifyEmail(java
	 * .lang.String)
	 */
	public boolean verifyEmail(String email) {
		// TODO Auto-generated method stub
		DcssUser user = userDAO.findByEmail(email);
		user.setIsEmailVerified(1);

		userDAO.update(user);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.flying.dcss.userpermissions.service.IUserService#modifyUser(com.flying
	 * .dcss.userpermissions.bo.impl.DcssUser)
	 */
	public boolean modifyUser(DcssUser user) {

		userDAO.update(user);
		return true;
	}

	public boolean addLoginRecord(DcssLoginRecord loginRecord) {
		loginRecordDAO.save(loginRecord);
		return true;
	}

	public List<DcssLoginRecord> getLoginRecordByUserId(String userId) {
		return loginRecordDAO.findByUserId(userId);
	}

	public DcssLoginRecord getLoginRecordById(String id) {
		return loginRecordDAO.findById(id);
	}

}
